
public class Main {
    public static void main(String[] args) {
        String ip = "172.16.254.01";

        System.out.println(validIPAddressAll(ip));
    }

    public static String validIPAddressAll(String IP) {

        if (!IP.contains(".") && !IP.contains(":")) {
            return "Neither";
        }
        if (IP.contains(".")) {
            if (IP.endsWith(".")) {
                return "Neither";
            }
            String[] arr = IP.split("\\.");
            if (arr.length != 4) {
                return "Neither";
            }

            for (int i = 0; i < 4; i++) {
                if (arr[i].length() == 0 || arr[i].length() > 3) {
                    return "Neither";
                }
                for (int j = 0; j < arr[i].length(); j++) {
                    if (arr[i].charAt(j) >= '0' && arr[i].charAt(j) <= '9') {
                        continue;
                    }
                    return "Neither";
                }
                if (Integer.valueOf(arr[i]) > 255 || (arr[i].length() >= 2 && String.valueOf(arr[i]).startsWith("0"))) {
                    return "Neither";
                }
            }
            return "IPv4";
        }
        return "IPv4";
    }
}





